package server

import (
	"encoding/json"
	"exam/db"
	"fmt"
	"net/http"

	"gorm.io/gorm"
	// "server/DB"
)

type student struct {
	ID   string
	Name string
	Age  string
}


func getStudents(DB *gorm.DB) []byte{
	var students  []student
	DB.Distinct("id", "name", "age").Limit(20).Find(&students) // 这里不用distinct，用select投影
	r, err := json.Marshal(&students)
	if  err != nil {
		fmt.Println("encode failed")
	}
	return r
}

// type studentbangdingle
func StudentFunc(w http.ResponseWriter,r *http.Request){
	w.Write(getStudents(db.DB)) // 如果服务器读取失败应考虑返回浏览器一个错误信息
}
